package org.lq.ht.view.employee;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import org.lq.ht.entity.Employee;
import org.lq.ht.service.EmployeeService;
import org.lq.ht.service.impl.EmployeeServiceImpl;
import org.lq.ht.util.CastUtil;
import org.lq.ht.util.CastUtils;
import org.lq.ht.util.ShowMessage;


import javax.swing.JButton;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Color;
import javax.swing.ImageIcon;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.List;
import java.awt.event.ActionEvent;

public class employeeMain extends JFrame {

	private JPanel contentPane;
	private JTable table;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					employeeMain frame = new employeeMain();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public employeeMain() {
		setTitle("员工资料管理");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 787, 496);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JScrollPane scrollPane = new JScrollPane();
		contentPane.add(scrollPane, BorderLayout.CENTER);
		
		table = new JTable();
		table.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"id", "\u7F16\u53F7", "\u59D3\u540D", "\u624B\u673A\u53F7", "\u5E95\u85AA", "\u5165\u804C\u65F6\u95F4", "\u5907\u6CE8"
			}
		));
		table.getColumnModel().getColumn(0).setMinWidth(1);
		table.getColumnModel().getColumn(0).setMaxWidth(1);
		scrollPane.setViewportView(table);
		
		JPanel panel = new JPanel();
		FlowLayout flowLayout = (FlowLayout) panel.getLayout();
		flowLayout.setHgap(20);
		contentPane.add(panel, BorderLayout.SOUTH);
		
		JButton bt_save = new JButton("增加");
		bt_save.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				emplyeeAdd add = new emplyeeAdd();
				add.setBounds(employeeMain.this.getX(), employeeMain.this.getY(), add.getWidth(), add.getHeight());
				add.setVisible(true);
			
				//监听窗口是否被关闭
				add.addWindowListener(new WindowAdapter() {
					@Override
					public void windowClosed(WindowEvent e) {
						//当窗口被关闭的时候进行初始化
						initTable();
					}
				});
			}
		});
		bt_save.setIcon(new ImageIcon(employeeMain.class.getResource("/images/add.png")));
		bt_save.setFont(new Font("宋体", Font.PLAIN, 20));
		panel.add(bt_save);
		
		JButton bt_update = new JButton("修改");
		bt_update.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int index = table.getSelectedRow();
				if(index>=0) {
					Object v = table.getValueAt(index, 0);
					int id = CastUtils.castInt(v);//拿到所选的行员工序号no
					emplyeeAdd empUpdate = new  emplyeeAdd(id);
					empUpdate.setBounds(employeeMain.this.getX(), employeeMain.this.getY(), empUpdate.getWidth(), empUpdate.getHeight());
					empUpdate.setVisible(true);//显示
					empUpdate.addWindowListener(new WindowAdapter() {
						public void windowClosed(WindowEvent e) {//当窗口关闭时就行表格初始化
							initTable();
							
						};
					});
				}
			}
		});
		bt_update.setIcon(new ImageIcon(employeeMain.class.getResource("/images/modify.png")));
		bt_update.setFont(new Font("宋体", Font.PLAIN, 20));
		bt_update.setBackground(new Color(240, 240, 240));
		panel.add(bt_update);
		
		JButton bt_dele = new JButton("删除");
		bt_dele.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int index = table.getSelectedRow();
				if(index>=0) {
					int n=JOptionPane.showConfirmDialog(employeeMain.this, "确定删除"+table.getValueAt(index, 2)+"员工吗?","删除",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
					if(n==0) {
						Object v = table.getValueAt(index, 0);
						int id = CastUtils.castInt(v);
						EmployeeService et = new EmployeeServiceImpl();
						ShowMessage.frameMessage(employeeMain.this, et.delete(id),"删除！");
						initTable();
					}
				}
			}
		});
		bt_dele.setIcon(new ImageIcon(employeeMain.class.getResource("/images/delete.png")));
		bt_dele.setFont(new Font("宋体", Font.PLAIN, 20));
		panel.add(bt_dele);
		initTable();
	}
	
	/**
	 * 表格初始化
	 */
	public void initTable() {
		DefaultTableModel dtm = (DefaultTableModel) table.getModel();
		int count = dtm.getRowCount();
		for (int i = 0; i < count; i++) {
			dtm.removeRow(0);
		}
		
		EmployeeService emService = new EmployeeServiceImpl();
		List<Employee> list=emService.findAll();
		for (Employee e : list) {
			dtm.addRow(new Object[] {
					e.getNo(),
					e.getEmployeeNum(),
					e.getName(),
					e.getPhoneNumber(),
					e.getSalary(),
					e.getHiredate(),
					e.getNote()
			});
		}
	}

}
